package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private  RoleMapper roleMapper;
    @Override
    @Transactional
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
        //删除角色权限中间表格信息
        roleMapper.deleteRoleAndPermissionRelation(id);
        //删除员工角色中间表信息
        roleMapper.deleteEmployeeAndRoleRelation(id);
    }

    @Override
    @Transactional
    public void insert(Role role, Long[] ids) {
        roleMapper.insert(role);
        //判断传入的ids是否为空
        if (ids != null && ids.length > 0) {
            //添加中间表
            roleMapper.insertRelation(role.getId(), ids);
        }
    }

    @Override
    public Role listOne(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> listAll() {
        return roleMapper.selectAll();
    }

    @Override
    @Transactional
    public void update(Role role, Long[] ids) {
        //先删除中间表信息
        roleMapper.updateByPrimaryKey(role);
        roleMapper.deleteRoleAndPermissionRelation(role.getId());
        if (ids != null && ids.length > 0) {
            //添加中间表
            roleMapper.insertRelation(role.getId(), ids);
        }
    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        //开始分页,对下一条SQL语句进行分页,传入当前页,每页显示的数据
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role>  data = roleMapper.selectForList(qo);
        return new PageInfo<Role>(data);
    }

}
